package com.dormitoryAdministratorSystem.service.student.impl;

import com.dormitoryAdministratorSystem.dao.student.impl.StudentDaoImpl;
import com.dormitoryAdministratorSystem.entity.page.PageBean;
import com.dormitoryAdministratorSystem.entity.student.StudentBean;
import com.dormitoryAdministratorSystem.service.room.impl.RoomSerImpl;
import com.dormitoryAdministratorSystem.service.student.IStudentService;
import com.dormitoryAdministratorSystem.util.DBUtil2;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class StudentSerImpl implements IStudentService {
    private StudentDaoImpl sdi = new StudentDaoImpl();
    private RoomSerImpl rsi = new RoomSerImpl();

    @Override
    public PageBean<StudentBean> showStusInfo(Map<String, String> con) {
        PageBean<StudentBean> p = new PageBean<>();
        String sql =  "select \n" +
                "\tsid,s.rid,sname,gender,sPhoneNo,photoPath,sTime,\n" +
                "\tc.cid as cid,cname,openTime,teacher \n" +
                "\tfrom t_student as s LEFT JOIN t_class as c on c.cid = s.cid " +
                "left join t_room as r on r.rid = s.rid " +
                "where 1 = 1 ";
        String countSql = "select count(*) as sNum from t_student as s LEFT JOIN t_class as c on s.cid = c.cid" +
                " left join t_room as r on r.rid = s.rid " +
                " where 1 = 1 ";
        List<String> list = new ArrayList<>();

        if(!"".equals(con.get("sname")) && con.get("sname") != null){
            sql += " and sname like ? ";
            countSql += " and sname like ? ";
            list.add("%" + con.get("sname") + "%");
        }

        if(!"".equals(con.get("className")) && con.get("className") != null){
            sql += " and cname = ?";
            countSql += " and cname = ?";
            list.add(con.get("className"));
        }

        if(!"".equals(con.get("rid") ) && con.get("rid") != null){
            sql += " and address like ? ";
            countSql += "and address like  ? ";
            list.add("%" + con.get("rid") + "%");
        }

        if("".equals(con.get("curPage")) || con.get("curPage") == null){
            p.setCurPage(1); // 如果没有说明当前页面，则默认是第一页
        }else{
            p.setCurPage(Integer.parseInt(con.get("curPage")));
        }

        if("".equals(con.get("pageSize")) || con.get("pageSize") == null){
            p.setPageSize(5); // 如果没有说明没有显示条数，则默认是5
        }else{
            p.setPageSize(Integer.parseInt(con.get("pageSize")));
        }

        Object[] parms1 = new Object[list.size()];
        Object[] parms2 = new Object[list.size() + 2];

        for (int i = 0; i < list.size(); i++) {
            parms1[i] = list.get(i);
            parms2[i] = list.get(i);
        }

//        System.out.println(countSql);
        p.setTolNum(sdi.getstudentTolNum(countSql,parms1));
        sql += " order by sid desc limit ?,?";
//       System.out.println(sql);
        parms2[parms1.length] = (p.getCurPage() - 1 ) * p.getPageSize();
        parms2[parms1.length+1] = p.getPageSize();
//        System.out.println("==" +parms2[parms1.length] + "=="+ parms2[parms1.length + 1]);
        p.setTolSize((int) Math.ceil((double)p.getTolNum() / p.getPageSize()));

        p.setDates(sdi.getStudents(sql,parms2));
        return p;
    }

    @Override
    public int addStuInRoom(StudentBean stu) {
        Connection con = DBUtil2.getConnection();
        int result = 0;
        try {
            con.setAutoCommit(false);
            result = sdi.insertStudentInRoom(stu);
            result = rsi.updateRoomInfo(stu.getRoomBean().getRId(),1);
            con.commit();
        } catch (SQLException e) {
            try {
                con.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        }finally {
            DBUtil2.close();
        }
        return result;
    }

    @Override
    public int checkOutRoom(int sId) {

        Connection con = DBUtil2.getConnection();
        int result = 0;
        try {
            con.setAutoCommit(false);
            StudentBean s = sdi.getStudentById(sId);
            result = rsi.updateRoomInfo(s.getRoomBean().getRId(),-1);
            s.getRoomBean().setRId(0);
            result = sdi.updateStudentInRoom(s);
            con.commit();
        } catch (SQLException e) {
            try {
                con.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        }finally {
            DBUtil2.close();
        }
        return result;
    }

    @Override
    public int changeRoom(StudentBean stu,int oldRid) {
        Connection con = DBUtil2.getConnection();
        int result = 0;
        try {
            con.setAutoCommit(false);
            sdi.updateStudentInRoom(stu);
            rsi.updateRoomInfo(oldRid, -1);
            rsi.updateRoomInfo(stu.getRoomBean().getRId(), 1);
            con.commit();
        } catch (SQLException e) {
            try {
                con.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        }finally {
            DBUtil2.close();
        }
        return result;
    }

    @Override
    public StudentBean showStuInfo(int sId) {
        return sdi.getStudentById(sId);
    }

    @Override
    public boolean haveStudentsInClass(int id) {
        List<StudentBean> list = sdi.getStudentByClassId(id);

        if(list.size() == 0){
            return false;
        }else{
            return true;
        }
    }

    @Override
    public List<StudentBean> haveStudentsInRoom(int rId) {
        return sdi.getStudentByRoomId(rId);
    }
}
